import Vue from "vue";
import Router from "vue-router";
import RouterView from "@/layout/routerView.vue";
Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true
  },

  {
    path: "/",
    component: Layout,
    redirect: "/onlineShop",
    children: [
      {
        path: "recommen",
        name: "recommenToyou",
        component: () => import("@/views/recommenToyou/index"),
        hidden: true,
        meta: { title: "为你推荐", icon: "iconfont icon-tuijianmoban" }
      }
    ]
  },
  {
    path: "/onlineShop",
    name: "ShopDecoration",
    redirect: "/onlineShop/list",
    component: Layout,
    meta: {
      title: "线上门店装修",
      icon: "beautiful",
      permission: []
    },
    children: [
      {
        path: "list",
        name: "onlineShopList",
        meta: {
          title: "线上门店装修",
          permission: []
        },
        component: () => import("@/views/onlineShop/list")
      }
    ]
  },
  {
    path: "/meiyaPosters",
    name: "meiyaPosters",
    meta: {
      title: "美业海报",
      icon: "iconfont icon-weihaibao",
      permission: []
    },
    redirect: "/meiyaPosters/list",
    component: Layout,
    children: [
      {
        path: "list",
        name: "meiyaPostersList",
        meta: {
          title: "美业海报",
          permission: []
        },
        component: () => import("@/views/meiyaPosters/list")
      }
    ]
  },
  {
    path: "/gdeditor/:id",
    name: "GdEditor",
    component: () => import(/* webpackChunkName: "GdEditor" */ "@/views/gdeditor/index")
  },
  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true }
];

const createRouter = () =>
  new Router({
    mode: "history",
    scrollBehavior: () => ({ y: 0 }), //滚动行为，前端路由，当切换新路由时候，想要页面滚动到顶部
    routes: constantRoutes
  });

const router = createRouter();

// 它的原理其实很简单，所有的 vue-router 注册的路由信息都是存放在matcher之中的，所以当我们想清空路由的时候，我们只要新建一个空的Router实例，将它的matcher重新赋值给我们之前定义的路由就可以了。巧妙的实现了动态路由的清除。
// 现在我们只需要调用resetRouter，就能得到一个空的路有实例，之后你就可以重新addRoutes你想要的路由了。
// 问题解决： 动态添加的路由，并不能动态的删除---还原初始化时候的 只用constant静态路由的状态
// hack 的方法，能很好的动态清除注册的路由
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter(); // 保存最开始只有constant静态路由的路由配置
  router.matcher = newRouter.matcher; // reset router
}

export default router;
